package com.zc.runner.web.controller;

import com.zc.runner.core.domain.Customer;
import com.zc.runner.core.repository.CustomerRepository;
import com.zc.runner.core.service.CustomerService;
import com.zc.runner.web.form.EditForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerRepository customerRepository;

//    //http://localhost:8089/customer/login
//    @GetMapping("/login")
//    public String login() {
//        return "customer/login";
//    }
//
//    @PostMapping("/login")
//    public String login(@RequestParam String username, @RequestParam String password) {
//        Customer customer = customerService.login(username, password);
//
//        if (customer == null) {
//            //登录不成功,跳转到错误页面
//            return "customer/error";
//        } else {
//            //把用户数据存放session中
//
//            //登录成功，跳转到首页
//            return "customer/index";
//        }
//
//    }

    @GetMapping("/profile")
    public String profile(HttpSession session, Model model){

        Customer customer = (Customer) session.getAttribute("customer");
        Customer customer1 = customerService.get(customer.getId());
        model.addAttribute("customer",customer);
        return "customer/profile";
    }

    @GetMapping("/edit")
    public String edit(){
        return "customer/edit";
    }

    @PostMapping("/edit")
    public String eidt(@Validated EditForm editForm,
                       BindingResult bindingResult,
                       Model model,
                       HttpSession session){

        if (bindingResult.hasErrors()){
            model.addAttribute("errorMsg",getErrorMessage(bindingResult));
            return "customer/edit";
        }
        Customer customer = (Customer) session.getAttribute("customer");
        customer.setMobile(editForm.getMobile());
        customer.setNickname(editForm.getNickname());
        customer.setPassword(editForm.getPassword());
        Customer result = customerService.update(customer);

        if (result != null) {
            //编辑成功
            session.setAttribute("customer",result);
            model.addAttribute("customer",result);
            return "customer/profile";
        } else {
            //编辑失败
            return null;
        }
    }

    @GetMapping("/apply")
    public String apply(HttpSession session, Model model){
        Customer customer = (Customer) session.getAttribute("customer");
        Customer result = customerService.applyToDistributor(customer.getId());

        model.addAttribute("customer", result);
        model.addAttribute("tip", "您已提交申请，请耐心等待");
        return "customer/profile";
    }


    private String getErrorMessage(BindingResult bindingResult){
        StringBuilder errorMsg = new StringBuilder();
        int i = 0;
        for (ObjectError error : bindingResult.getAllErrors()) {
            if (i != 0) {
                errorMsg.append("<br/>");
            }
            errorMsg.append(error.getDefaultMessage());
            i++;
        }
        return errorMsg.toString();
    }
}
